package org.hdz.changelanguage;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

/**
 * @author hdz
 * @date 2019/01/05
 */
public class LanguageActivity extends AppCompatActivity {

    private int mOldLangId = 0;
    private int mNewLangId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_language);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        mOldLangId = UserConfig.getLanguageId(this);

        List<String> mData = new LinkedList<String>();
        mData.add(getResources().getString(R.string.auto));
        mData.add("简体中文");
        mData.add("繁體中文");
        mData.add("English");
        mData.add("日本語");

        final LanguageAdapter adapter = new LanguageAdapter(this, mData);
        ListView listView = (ListView)findViewById(R.id.languageList);
        listView.setAdapter(adapter);

        adapter.setChecked(UserConfig.getLanguageId(this));
        adapter.notifyDataSetChanged();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(LanguageActivity.this, adapter.getItem(position).toString(), Toast.LENGTH_SHORT).show();
                adapter.setChecked(position);
                // 重绘
                adapter.notifyDataSetChanged();
                mNewLangId = position;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.onBackPressed();
                return true;
            case R.id.save:
                if (mNewLangId != mOldLangId) {
                    UserConfig.setLanguageId(LanguageActivity.this, mNewLangId);
                    android.os.Process.killProcess(android.os.Process.myPid());
                    System.exit(0);
                }
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.save_language, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
